package io.kiki.sba.registry.common.model.slot;


import io.kiki.sba.registry.common.model.ClientInterfaceId;
import io.kiki.sba.registry.common.model.store.Publisher;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.*;


public class DataSlotDiffPublisherResult implements Serializable {
    private final Map<String, List<Publisher>> updatedPublishers;
    private final Map<String, List<String>> removedPublishers;
    // contains all the updated/added publishers
    @Getter
    private final boolean hasRemain;
    @Setter
    @Getter
    private long slotsEpoch;
    // if from session, return the sessionProcessId for lease
    @Setter
    @Getter
    private ClientInterfaceId clientInterfaceId;

    public DataSlotDiffPublisherResult(boolean hasRemain, Map<String, List<Publisher>> updatedPublishers, Map<String, List<String>> removedPublishers) {
        this.updatedPublishers = new HashMap<>(updatedPublishers);
        this.removedPublishers = new HashMap<>(removedPublishers);
        this.hasRemain = hasRemain;
    }


    public Map<String, List<Publisher>> getUpdatedPublishers() {
        return Collections.unmodifiableMap(updatedPublishers);
    }


    public Map<String, List<String>> getRemovedPublishers() {
        return Collections.unmodifiableMap(removedPublishers);
    }

    public int getRemovedPublishersCount() {
        int count = 0;
        for (List<String> list : removedPublishers.values()) {
            count += list.size();
        }
        return count;
    }

    public int getUpdatedPublishersCount() {
        int count = 0;
        for (List<Publisher> list : updatedPublishers.values()) {
            count += list.size();
        }
        return count;
    }


    public boolean isEmpty() {
        return updatedPublishers.isEmpty() && removedPublishers.isEmpty();
    }

    public Set<String> syncDataInfoIds() {
        Set<String> ret = new HashSet<>();
        ret.addAll(updatedPublishers.keySet());
        ret.addAll(removedPublishers.keySet());
        return ret;
    }
}
